package com.hgts.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgts.common.R;
import com.hgts.mapper.CommentMapper;
import com.hgts.mapper.LikesMapper;
import com.hgts.pojo.Comment;
import com.hgts.pojo.Likes;
import com.hgts.service.ICommentService;
import com.hgts.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-05-14
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private LikesMapper likesMapper;
    /*
     * 评论区
     * */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ICommentService commentService;
    @Override
    public List<Comment> comment(Long bookId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id",bookId).orderByDesc("likes").orderByDesc("create_time");
        //查询comment是否被点赞
        /*Integer userId = UserHolder.getUserWx().getId();
        Comment comment = getById(userId);
        String key1 = "blog:liked:" + userId;
        Boolean isNumber1 = stringRedisTemplate.opsForSet().isMember(key1,userId.toString());
        comment.setIsLike(BooleanUtil.isTrue(isNumber1));*/
        System.err.println(commentMapper.selectList(queryWrapper));
        return commentMapper.selectList(queryWrapper);
    }
    @Transactional
    @Override
    public R<Comment> likeComment(Long commentId) {
        //1.判断当前登录用户
        Integer userId = UserHolder.getUserWx().getId();
        //2.判断当前用户是否点赞
        String key = "blog:liked:" + commentId;
        Boolean isNumber = stringRedisTemplate.opsForSet().isMember(key,userId.toString());
        if (BooleanUtil.isFalse(isNumber)) {
            //3.如果未点赞,则可以点赞
            //3.1 数据库点赞 +1
            boolean isSuccess = update().setSql("likes = likes +1").eq("comment_id", commentId).update();
            Likes likes = new Likes();
            likes.setUserId(userId);
            likes.setCommentId(commentId);
            likes.setLike(true);
            likesMapper.insert(likes);
            //3.2 保存用户到Redis的set集合
            if (isSuccess) {
                stringRedisTemplate.opsForSet().add(key, userId.toString());

            }
        }else {
            //4. 如果已经点赞，取消点赞
            //4.1 数据库点赞 -1
            boolean isSuccess = update().setSql("likes = likes - 1").eq("comment_id", commentId).update();
            likesMapper.removeById(commentId);
            //4.2 把Redis的set集合移除
            if (isSuccess) {
                stringRedisTemplate.opsForSet().remove(key,userId.toString());
            }
        }
        //查询comment是否被点赞
        Comment comment = commentMapper.getById(commentId);
        Boolean isNumber1 = stringRedisTemplate.opsForSet().isMember(key,userId.toString());
        comment.setIsLike(BooleanUtil.isTrue(isNumber1));
        return R.success(comment);
    }

}
